/**
 * 
 */
package com.cqan.account.controller;

import java.util.Calendar;
import java.util.List;
import java.util.StringTokenizer;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import static com.cqan.common.ViewName.*;
import com.cqan.account.entity.Authority;
import com.cqan.account.entity.Authority.AuthType;
import com.cqan.account.service.AuthorityService;
import com.cqan.controller.BaseController;
import com.cqan.exception.AppException;
import com.cqan.orm.PropertyFilter;
import com.google.common.collect.Lists;

/**
 * @Description TODO
 * @author slaton.wu@gmail.com
 * @date 2012-6-29下午7:39:01
 *
 */
@Controller
@RequestMapping("/auth")
public class AuthorityController extends BaseController<Authority> {

	@Resource(name="authorityService")
	private AuthorityService authorityService;
	
	/**
	 * 权限管理列表
	 * @param pageNo
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/list.htm")
	public ModelAndView list(Integer pageNo,HttpServletRequest request)throws AppException{
		ModelAndView mav = new ModelAndView(AUTH_LIST);
		List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(request);
		page.setPageNo(pageNo);
		page = authorityService.search(page, filters);
		mav.addObject("page", page);
		return mav;
	}
	
	@RequestMapping(value="/edit.htm")
	public ModelAndView edit(Long id,int pageNo)throws AppException{
		ModelAndView mav = new ModelAndView(AUTH_EDIT);
		List<Authority> auths = authorityService.getTopMenus();
		if (id!=null&&id>0) {
			Authority auth = authorityService.get(id);
			auths.remove(auth);
			mav.addObject("auth", auth);
		}
		mav.addObject("pageNo",pageNo);
		mav.addObject("auths", auths);
		return mav;
	}
	
	@RequestMapping(value="/save.htm",method=RequestMethod.POST)
	public ModelAndView save(HttpServletRequest request,Long id)throws AppException{
		ModelAndView mav = new ModelAndView(new RedirectView("list.htm?pageNo="+request.getParameter("pageNo")));
		try {
			Authority auth = getRequestEntity(request);
			String pid = request.getParameter("pid");
			Long parentId = Long.parseLong(pid);
			Authority parent = authorityService.get(parentId);
			auth.setParent(parent);
			String authType = request.getParameter("authType");
			auth.setAuthType(AuthType.valueOf(AuthType.class, authType));
			if (id!=null) {
				Authority a = authorityService.get(id);
				copyProperties(a, auth);
				authorityService.update(a);
				return mav;
			}
			auth.setCteateTime(Calendar.getInstance().getTime());
			authorityService.add(auth);
		} catch (Exception e) {
			e.printStackTrace();
			throw new AppException();
		}
		return mav;
	}
	
	@ResponseBody
	@RequestMapping(value="/delete.htm",method=RequestMethod.POST)
	public String delete(String ids)throws AppException{
		try {
			if (ids!=null) {
				StringTokenizer st = new StringTokenizer(ids,"-");
				List<Long> list = Lists.newArrayList();
				while (st.hasMoreTokens()) {
					String id = st.nextToken();
					if (id!=null&&!"".equals(id)) {
						list.add(Long.parseLong(id));
					}
				}
				authorityService.delete(list);
				return "删除成功！";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "该权限已有角色正在使用，暂不能删除！";
		}
		return  "删除失败！";
	}
}
